package com.ruoyi.controller.equipment;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.equipment.MesEquipment;
import com.ruoyi.service.equipment.IMesEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/equipment/equipment")
public class MesEquipmentController extends BaseController
{
    @Autowired
    private IMesEquipmentService IMesEquipmentService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesEquipment mesEquipment)
    {
        startPage();
        List<MesEquipment> list = IMesEquipmentService.selectMesEquipmentList(mesEquipment);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesEquipment mesEquipment)
    {
        List<MesEquipment> list = IMesEquipmentService.selectMesEquipmentList(mesEquipment);
        ExcelUtil<MesEquipment> util = new ExcelUtil<MesEquipment>(MesEquipment.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(IMesEquipmentService.selectMesEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesEquipment mesEquipment)
    {
        // 校验设备编码唯一性
        if ("1".equals(IMesEquipmentService.checkEquipmentCodeUnique(mesEquipment.getEquipmentCode())))
        {
            return error("新增设备失败，设备编码已存在");
        }
        return toAjax(IMesEquipmentService.insertMesEquipment(mesEquipment));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesEquipment mesEquipment)
    {
        // 校验设备编码唯一性（排除自身）
        MesEquipment existEquipment = IMesEquipmentService.selectMesEquipmentByEquipmentId(mesEquipment.getEquipmentId());
        if (existEquipment != null && !existEquipment.getEquipmentCode().equals(mesEquipment.getEquipmentCode()))
        {
            if ("1".equals(IMesEquipmentService.checkEquipmentCodeUnique(mesEquipment.getEquipmentCode())))
            {
                return error("修改设备失败，设备编码已存在");
            }
        }
        return toAjax(IMesEquipmentService.updateMesEquipment(mesEquipment));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(IMesEquipmentService.deleteMesEquipmentByEquipmentIds(equipmentIds));
    }

    /**
     * 校验设备编码
     */
    @PostMapping("/checkEquipmentCodeUnique")
    public AjaxResult checkEquipmentCodeUnique(@RequestBody MesEquipment mesEquipment)
    {
        String result = IMesEquipmentService.checkEquipmentCodeUnique(mesEquipment.getEquipmentCode());
        if ("1".equals(result))
        {
            return error("设备编码已存在");
        }
        return success();
    }

    /**
     * 根据设备类型查询设备列表
     */
    @GetMapping("/listByType/{equipmentType}")
    public AjaxResult listByType(@PathVariable String equipmentType)
    {
        MesEquipment mesEquipment = new MesEquipment();
        mesEquipment.setEquipmentType(equipmentType);
        List<MesEquipment> list = IMesEquipmentService.selectMesEquipmentList(mesEquipment);
        return success(list);
    }

    /**
     * 根据设备状态查询设备列表
     */
    @GetMapping("/listByStatus/{status}")
    public AjaxResult listByStatus(@PathVariable String status)
    {
        MesEquipment mesEquipment = new MesEquipment();
        mesEquipment.setStatus(status);
        List<MesEquipment> list = IMesEquipmentService.selectMesEquipmentList(mesEquipment);
        return success(list);
    }

    /**
     * 获取正常状态的设备列表
     */
    @GetMapping("/listAvailable")
    public AjaxResult listAvailable()
    {
        MesEquipment mesEquipment = new MesEquipment();
        mesEquipment.setStatus("0"); // 0表示正常状态
        List<MesEquipment> list = IMesEquipmentService.selectMesEquipmentList(mesEquipment);
        return success(list);
    }
}